package com.lazar.ke.encrypt;

import org.apache.commons.lang3.StringUtils;
import org.mindrot.jbcrypt.BCrypt;

public class BCryptUtil {

    /**
     * bcrypt加密
     * @param password 加密字符串
     * @return
     */
    public static String encrypt(String password){

        if (StringUtils.isEmpty(password))return null;
        String hashed = BCrypt.hashpw(password, BCrypt.gensalt());
        //或者使用如下
        //String hashed2 = BCrypt.hashpw(password, BCrypt.gensalt(12));
        return hashed;
    }

    /**
     * bcrypt密码验证
     * @param password        明文密码
     * @param encryptPassword 加密密码
     * @return
     */
    public static boolean authenticate(String password,String encryptPassword){
        if (StringUtils.isEmpty(password) || StringUtils.isEmpty(encryptPassword))return false;
        if (BCrypt.checkpw(password, encryptPassword)){
            return true;
        }
        return false;
    }
}
